/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.fenfen.novel.utils;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.WindowManager;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
class SystemUiHelperImplHC extends SystemUiHelper.SystemUiHelperImpl
    implements View.OnSystemUiVisibilityChangeListener {

  final View mDecorView;

  SystemUiHelperImplHC(Activity activity, int level, int flags,
                       SystemUiHelper.OnVisibilityChangeListener onVisibilityChangeListener) {
    super(activity, level, flags, onVisibilityChangeListener);

    mDecorView = activity.getWindow().getDecorView();
    mDecorView.setOnSystemUiVisibilityChangeListener(this);

    if (mLevel <= SystemUiHelper.LEVEL_HIDE_STATUS_BAR) {
      mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
      mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
    }
  }


  @Override
  void show() {
    mDecorView.setSystemUiVisibility(createShowFlags());
  }

  @Override
  void hide() {
    mDecorView.setSystemUiVisibility(createHideFlags());
  }

  @Override
  public final void onSystemUiVisibilityChange(int visibility) {
    if ((visibility & createTestFlags()) != 0) {
      onSystemUiHidden();
    } else {
      onSystemUiShown();
    }
  }

  protected void onSystemUiShown() {
    if (mLevel == SystemUiHelper.LEVEL_LOW_PROFILE) {
      // Manually hide the action bar when in low profile mode.
      ActionBar ab = mActivity.getActionBar();
      if (ab != null) {
        ab.show();
      }
    }

    if (mLevel <= SystemUiHelper.LEVEL_HIDE_STATUS_BAR) {
      mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    } else {
      mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

    setIsShowing(true);
  }

  protected void onSystemUiHidden() {
    if (mLevel == SystemUiHelper.LEVEL_LOW_PROFILE) {
      ActionBar ab = mActivity.getActionBar();
      if (ab != null) {
        ab.hide();
      }
    }

    if (mLevel <= SystemUiHelper.LEVEL_HIDE_STATUS_BAR) {
      mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    setIsShowing(false);
  }

  protected int createShowFlags() {
    return View.STATUS_BAR_VISIBLE;
  }

  protected int createHideFlags() {
    return View.STATUS_BAR_HIDDEN;
  }

  protected int createTestFlags() {
    return View.STATUS_BAR_HIDDEN;
  }
}
